package com.tocea.formation.tpLibraryEjb.eao;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.tocea.formation.tpLibraryEjb.entities.Author;
import com.tocea.formation.tpLibraryEjb.entities.Book;
import com.tocea.formation.tpLibraryEjb.exception.BookNotFoundException;

@Stateless
@LocalBean
public class BookEAO {

	@PersistenceContext
	private EntityManager em;

	private AuthorEAO aEAO = new AuthorEAO();

	public Book getBook(String _isbn){
		Query q = em.createQuery("Select b from Book b where isbn like '"+_isbn+"'" );
		return (Book) q.getSingleResult();
	}
	
	public List<Book> findBookByName(String s) {
		Query q = em.createQuery("from Book b where firstname like '%" + s
				+ "%' or lastname like '%" + s + "%' ");
		@SuppressWarnings("unchecked")
		List<Book> results = (List<Book>) q.getResultList();
		return results;
	}

	public List<Book> findBookByAuthorName(String s) {
		Query q = em
				.createQuery("from Book b join Author a on a.id = b.author where b.name like '"
						+ s + "' or b.isbn like '" + s + "' ");
		@SuppressWarnings("unchecked")
		List<Book> results = q.getResultList();
		return results;
	}

	public void addBook(String nom, String genre, String _firstNameAuthor,
			String _lastNameAuthor, String isbn, Integer exemplaires) {
		if (exists(isbn) == false) {
			Book a = new Book();
			a.setIsbn(isbn);
			a.setExemplaires(exemplaires);
			a.setGenre(genre);
			a.setName(nom);
			Author auth = aEAO.getAuthor(_firstNameAuthor, _lastNameAuthor);
			if (auth != null) {
				a.setAuthor(auth);
			}
			em.persist(a);
		} else {
			updateBook(isbn, nom, genre, exemplaires);
		}
	}

	public Integer updateBook(String isbn, String _name, String _genre,
			Integer ex) {
		Query q = em.createQuery("update Book set name = '" + _name
				+ "', genre = '" + _genre + "', exemplaires = " + ex
				+ " where isbn like '" + isbn + "' ");
		return q.executeUpdate();
	}

	public Integer deleteBook(String isbn){
		Query q = em.createQuery("delete from Book where isbn like '" + isbn + "' ");
		return q.executeUpdate();
	}

	public List<Book> getAll() {
		Query q = em.createQuery("from Book a");
		@SuppressWarnings("unchecked")
		List<Book> Books = q.getResultList();
		return Books;
	}

	public boolean exists(String isbn) {
		Query q = em.createQuery("select count(*) from Book b where isbn = '"
				+ isbn + "'");
		return q.getFirstResult() == 1;
	}

	public void addExemplaire(Book b) {
		updateBook(b.getIsbn(), b.getName(), b.getGenre(), b.getExemplaires());
	}
	
	public void removeExemplaire(String isbn, Integer nb) throws BookNotFoundException{
		Book b = getBook(isbn);
		if(b != null){
			updateBook(b.getIsbn(), b.getName(), b.getGenre(), (b.getExemplaires() - nb) );
		}else{
			throw new BookNotFoundException("ISBN INVALIDE");
		}
	}
	public void addExemplaire(String isbn, Integer nb) throws BookNotFoundException{
		Book b = getBook(isbn);
		if(b != null){
			updateBook(b.getIsbn(), b.getName(), b.getGenre(), (b.getExemplaires() + nb) );
		}else{
			throw new BookNotFoundException("ISBN INVALIDE");
		}
	}

}
